﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

using CRM.Entity.Extra;
using CRM.Entity;
using CRM.Dictionary.Box;
using CRM.Entity.Page;


namespace CRM.Dictionary.Service
{
    public partial class Sys_UserInfo_DicService
    {
        public static ReaderWriterLockSlim Sys_UserInfo_rws = new ReaderWriterLockSlim();

        public Sys_UserInfoPage Sys_UserInfo_List()
        {
            try
            {
                Sys_UserInfo_rws.EnterReadLock();

                IEnumerable<Sys_UserInfo> listEntity = DictionaryBox.Sys_UserInfoDictionary.Values;

                var Sys_UserInfoPage = new Sys_UserInfoPage()
                {
                    ListEntity = listEntity.ToList(),
                    RecordCount = listEntity.Count()
                };

                return Sys_UserInfoPage;
            }
            finally
            {
                Sys_UserInfo_rws.ExitReadLock();
            }
        }

        public Sys_UserInfoPage Sys_UserInfo_List(PageInfo page)
        {
            try
            {
                Sys_UserInfo_rws.EnterReadLock();

                IEnumerable<Sys_UserInfo> listEntity = DictionaryBox.Sys_UserInfoDictionary.Values;

                var Sys_UserInfoPage = new Sys_UserInfoPage()
                {
                    ListEntity = listEntity.AsParallel().OrderByDescending(i => i.user_id)
                                  .Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).ToList(),
                    RecordCount = listEntity.AsParallel().Count()
                };

                return Sys_UserInfoPage;
            }
            finally
            {
                Sys_UserInfo_rws.ExitReadLock();
            }
        }
        
        public Sys_UserInfoPage Sys_UserInfo_List(Func<Sys_UserInfo, bool> func)
        {
            try
            {
                Sys_UserInfo_rws.EnterReadLock();

                IEnumerable<Sys_UserInfo> listEntity = DictionaryBox.Sys_UserInfoDictionary.Values;

                var Sys_UserInfoPage = new Sys_UserInfoPage()
                {
                    ListEntity = listEntity.Where(func).ToList(),
                    RecordCount = listEntity.Count()
                };

                return Sys_UserInfoPage;
            }
            finally
            {
                Sys_UserInfo_rws.ExitReadLock();
            }
        }

        public Sys_UserInfoPage Sys_UserInfo_List(PageInfo page, Func<Sys_UserInfo, bool> func)
        {
            try
            {
                Sys_UserInfo_rws.EnterReadLock();

                IEnumerable<Sys_UserInfo> listEntity = DictionaryBox.Sys_UserInfoDictionary.Values
                                                                .Where(func);

                var Sys_UserInfoPage = new Sys_UserInfoPage()
                {
                    ListEntity = listEntity.AsParallel().OrderByDescending(i => i.user_id)
                                  .Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).ToList(),
                    RecordCount = listEntity.AsParallel().Count()
                };

                return Sys_UserInfoPage;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Sys_UserInfo_rws.ExitReadLock();
            }
        }

        public List<Sys_UserInfo> Sys_UserInfo_List_Top(int top, Func<Sys_UserInfo, bool> func)
        {
            try
            {
                Sys_UserInfo_rws.EnterReadLock();

                IEnumerable<Sys_UserInfo> listEntity = DictionaryBox.Sys_UserInfoDictionary.Values.Where(func);

                return listEntity.AsParallel().OrderByDescending(i => i.user_id).Take(top).ToList();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Sys_UserInfo_rws.ExitReadLock();
            }
        }

        public Sys_UserInfo Sys_UserInfo_Multiple(Func<Sys_UserInfo, bool> func)
        {
            try
            {
                Sys_UserInfo_rws.EnterReadLock();

                return DictionaryBox.Sys_UserInfoDictionary.Values.Where(func).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Sys_UserInfo_rws.ExitReadLock();
            }
        }

        public Sys_UserInfo Sys_UserInfo_Single(Int32 Sys_UserInfoID)
        {
            if(DictionaryBox.Sys_UserInfoDictionary.ContainsKey(Sys_UserInfoID))
                return DictionaryBox.Sys_UserInfoDictionary[Sys_UserInfoID];
            return null;
        }

        public bool Sys_UserInfo_Add(Sys_UserInfo Sys_UserInfo)
        {
            try
            {
                Sys_UserInfo_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.Sys_UserInfoDictionary.TryAdd(Sys_UserInfo.user_id, Sys_UserInfo);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Sys_UserInfo_rws.ExitWriteLock();
            }
        }
        
        public bool Sys_UserInfo_AddBatch(List<Sys_UserInfo> list)
        {
            try
            {
                Sys_UserInfo_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                    isSuccess = true;

                foreach (var item in list)
                    isSuccess = DictionaryBox.Sys_UserInfoDictionary.TryAdd(item.user_id, item);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Sys_UserInfo_rws.ExitWriteLock();
            }
        }

        public bool Sys_UserInfo_Update(Sys_UserInfo Sys_UserInfo)
        {
            try
            {
                Sys_UserInfo_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.Sys_UserInfoDictionary.TryUpdate(Sys_UserInfo.user_id,
                                          Sys_UserInfo, DictionaryBox.Sys_UserInfoDictionary[Sys_UserInfo.user_id]);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Sys_UserInfo_rws.ExitWriteLock();
            }
        }
        
        public bool Sys_UserInfo_UpdateBatch(List<Sys_UserInfo> list)
        {
            try
            {
                Sys_UserInfo_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                   isSuccess = true;

                foreach (var single in list)
                {
                    isSuccess = DictionaryBox.Sys_UserInfoDictionary.TryUpdate(single.user_id,
                                              single, DictionaryBox.Sys_UserInfoDictionary[single.user_id]);
                }

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Sys_UserInfo_rws.ExitWriteLock();
            }
        }

        public bool Sys_UserInfo_Remove(Sys_UserInfo Sys_UserInfo)
        {
            try
            {
                Sys_UserInfo_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.Sys_UserInfoDictionary.TryRemove(Sys_UserInfo.user_id, out Sys_UserInfo);

                return isSuccess;
            }
            finally
            {
                Sys_UserInfo_rws.ExitWriteLock();
            }
        }
        
        public bool Sys_UserInfo_RemoveList(List<Int64> list)
        {
            try
            {
                Sys_UserInfo_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                    isSuccess = true;

                foreach (var id in list)
                {
                    Sys_UserInfo single;

                    isSuccess = DictionaryBox.Sys_UserInfoDictionary.TryRemove(id, out single);
                }

                return isSuccess;
            }
            finally
            {
                Sys_UserInfo_rws.ExitWriteLock();
            }
        }
    }
}
